package com.nondo.rdp.finance.endpoint.api.admin.setting.course;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.rdp.auth.common.RoleUtils;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.course.CourseGroup;
import com.nondo.rdp.finance.service.course.CourseGroupService;

@RestController("apiAdminCourseGroup")
@RequestMapping("/api/admin/courseGroup")
public class CourseGroupEndpoint {
    @Autowired
    CourseGroupService courseGroupService;
    
    /**
     * 查看课程组
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/info/{id}")
	public View info(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("id") Long id) {
		View<CourseGroup> view = new View<CourseGroup>(20000, "获取数据成功");
		
		CourseGroup courseGroup = null;
		if (id != null) {
			courseGroup = courseGroupService.findOne(id);
		} else {
			courseGroup = new CourseGroup();
		}

		view.setData(courseGroup);
		return view;
	}
    
    /**
     * 添加课程组
     * @param request
     * @param response
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
	@RequestMapping("/add")
	public View add(HttpServletRequest request, HttpServletResponse response,
			@Validated @RequestBody CourseGroup courseGroup, BindingResult result) {
		View view = new View(20000, "添加成功");

		if (result.hasErrors()) {
			view.setCode(40000);
			view.setMessage(ValidateUtil.first(result.getAllErrors()));
		} else {
			this.courseGroupService.save(courseGroup);
		}
		return view;
	}

    /**
     * 更新课程组
     * @param request
     * @param response
     * @param id
     * @return
     */
	@PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
	@RequestMapping("/update")
	public View update(HttpServletRequest request,
			HttpServletResponse response,
			@Validated @RequestBody CourseGroup courseGroup, BindingResult result) {
		View view = new View(20000, "更新成功");

		if (result.hasErrors()) {
			view.setCode(40000);
			view.setMessage(ValidateUtil.first(result.getAllErrors()));
		} else {
			CourseGroup courseGroupDb = courseGroupService.findOne(courseGroup.getId());
			BeanUtils.copy(courseGroup, courseGroupDb);
			this.courseGroupService.update(courseGroupDb);
		}

		return view;
	}

    /**
     * 获取课程组列表
     * @param request
     * @param response
     * @param query
     * @return
     */
	@PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
    @RequestMapping("/list")
    public View<Page> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<CourseGroup> query) {
        View<Page> view = new View(20000, "获取列表成功");

        CourseGroup courseGroup = query.getData();

        Page<CourseGroup> page = courseGroupService.findAll(new Page<CourseGroup>(query.getPage(), query.getSize()), courseGroup);

        view.setData(page);

        return view;
    }

    /**
     * 删除课程组
     * @param request
     * @param response
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "删除成功");

        this.courseGroupService.delete(id);

        return view;
    }
}